<template>
	<view>
		<view style="padding: 5px 10px">
			<u--form
				:model="formData"
				labelWidth="90"
				ref="uForm"
				class="form"
				errorType="toast">
				<view class="plan card record-remarks record-remarks1">
					<!-- 检验项目编码 -->
					<u-form-item
						label=""
						prop="checkItemName"
						@click="getInspectionItems"
						ref="item1"
						borderBottom>
						<u-input
							v-model="formData.checkItemName"
							placeholder="请选择检验项目"
							@click="getInspectionItems"
							disabledColor="#ffffff"
							disabled
							border="none"
							height="40px"></u-input>
						<template #right>
							<u-icon name="arrow-right"></u-icon>
						</template>
					</u-form-item>
				</view>
			</u--form>
		</view>
		<!-- tab-bar切换 -->
		<view class="tab-bar">
			<u-tabs
				:current="activeTab"
				:list="tabList"
				:scrollable="false"
				:activeStyle="{
					color: '#000',
					fontWeight: 'bold',
					transform: 'scale(1.04)',
				}"
				lineWidth="50px"
				lineHeight="2px"
				lineColor="#007aff"
				@change="changeTab"></u-tabs>
		</view>

		<view v-show="activeTab === 0 && formData.checkItemName" class="addBox">
			<view class="addBoxMaterial">
				<!-- 条码录入 -->
				<view class="addBoxMaterialTitle">物料编号</view>

				<view class="searchScan_div">
					<scan-bar
						ref="scanBarMaterialRef"
						placeholder="请扫描或输入物料编号"
						@code-scanned="getScanResultMaterial"
						:key="dateMaterialkey"></scan-bar>
				</view>
			</view>

			<view class="addBoxBatch" v-if="taskInfo.needMaterialBatch">
				<!-- 条码录入 -->
				<view class="addBoxBatchTitle">批次号</view>

				<view class="searchScan_div">
					<scan-bar
						ref="scanBarBatchRef"
						placeholder="请扫描或输入批次号"
						@code-scanned="getScanResultBatch"
						:key="dateBatchkey"></scan-bar>
				</view>
			</view>
			<view class="addBoxSerial" v-if="taskInfo.needMaterialSerial">
				<!-- 条码录入 -->
				<view class="addBoxSerialTitle">序列号</view>

				<view class="searchScan_div">
					<scan-bar
						ref="scanBarSerialRef"
						placeholder="请扫描或输入序列号"
						@code-scanned="getScanResultSerial"
						:key="dateSerialkey"></scan-bar>
				</view>
			</view>
			<view class="addBoxProcess" v-if="taskInfo.needProcess">
				<!-- 条码录入 -->
				<!-- <view
					style="
						font-size: 15px;
						margin-left: 15px;
						margin-right: 5px;
					">
					工序
				</view>

				<view class="searchScan_div">
					<scan-bar
						ref="scanBarSerialRef"
						placeholder="请扫描或输入序列号"
						@code-scanned="getScanResultSerial"
						:key="dateSerialkey"></scan-bar>
				</view> -->
				<view class="addBoxProcessTitle">工序</view>
				<view style="width: 100%">
					<u--form
						:model="taskInfo"
						labelWidth="0"
						ref="uForm1"
						class="form"
						errorType="toast">
						<view class="plan card record-remarks record-remarks1">
							<!-- 工序 -->
							<u-form-item
								label=""
								prop="processName"
								@click="getProcessData"
								ref="item2"
								borderBottom>
								<u-input
									v-model="taskInfo.processName"
									placeholder="请选择工序"
									@click="getProcessData"
									disabledColor="#ffffff"
									disabled
									border="none"
									height="40px"></u-input>
								<template #right>
									<u-icon name="arrow-right"></u-icon>
								</template>
							</u-form-item>
						</view>
					</u--form>
				</view>
			</view>
			<view v-if="taskInfo.needEquip" class="addBoxEquip">
				<!-- 条码录入 -->
				<view class="addBoxEquipTitle">设备</view>

				<view class="searchScan_div">
					<scan-bar
						ref="scanBarEquipRef"
						placeholder="请扫描或输入设备"
						@code-scanned="getScanResultEquip"
						:key="dateEquipkey"></scan-bar>
				</view>
			</view>
			<view class="addBoxTitle">检验项</view>
			<view style="margin-bottom: 50px">
				<view
					class="overhidden"
					v-for="(item, index) in taskInfo?.details"
					:key="index">
					<view>
						<view class="record-content">
							<view class="record-item">
								<view class="title_new">
									{{ item?.sopItemName || "" }}
									<u-icon
										v-if="item?.sopItem.sopItemMethod"
										name="info-circle-fill"
										color="rgb(192, 196, 204)"
										size="18"
										@click="
											openInfoModal(
												item.sopItem.sopItemMethod
											)
										"></u-icon>
								</view>
							</view>
							<!-- <view class="record-item" v-if="item.sopItem.standardValue == 1 && !(!item.sopItem.standardMinValue && !item.sopItem.standardMaxValue)">
											<text>上下限值</text>
											<text v-if="item?.sopItem.standardMinValue !== item?.sopItem.standardMaxValue">
												{{ item?.sopItem.standardMinValue || '' }}-{{ item?.sopItem.standardMaxValue || '' }}{{ item?.standardValueUnit || '' }}
											</text>
											<text v-else>{{ item?.sopItem.standardMinValue || '' }}{{ item?.sopItem.standardValueUnit || '' }}</text>
										</view> -->
							<view
								class="record-item"
								v-if="item.sopItemStandard">
								<text>标准要求</text>
								<text>{{ item.sopItemStandard }}</text>
							</view>
							<view
								class="record-item"
								v-if="item.sopItem.standardValue == 1">
								<view class="flex">
									实际值
									<text
										class="require_icon"
										v-if="item?.sopItem.sopExecute == '1'">
										*
									</text>
								</view>
								<u--input
									v-if="props.isRead == 0"
									v-model="item.sopItemValue"
									placeholder="请输入"
									class="minInput"
									:class="
										sizeColorHandle(
											item.sopItemValue,
											item?.sopItem.standardMinValue,
											item?.sopItem.standardMaxValue
										)
									"
									inputAlign="right"
									clearable
									border="none"></u--input>
								<text
									v-else
									:class="
										sizeColorHandle(
											item.sopItemValue,
											item?.sopItem.standardMinValue,
											item?.sopItem.standardMaxValue
										)
									">
									{{ item.sopItemValue || "" }}
								</text>
							</view>
							<view
								class="record-item"
								v-if="item.sopItem.standardValue == 2">
								<view class="flex">
									实际值
									<text
										class="require_icon"
										v-if="item?.sopItem.sopExecute == '1'">
										*
									</text>
								</view>
								<view
									v-if="props.isRead == 0"
									@click="
										hideKeyboard(
											index,
											item.sopItem.details ?? []
										)
									"
									class="flex input-box">
									
									<view
										class="minInput"
										style="width:400rpx"
										:class="sizeColorHandle1(item.sopItemValueId, item.sopItem.details ?? [])"
										@click.stop="hideKeyboard(index, item.sopItem.details ?? [])"
										
									>
										<view class=""  >
											{{ item.sopItemValue? item.sopItemValue:'' }}
										</view>
										<view class="" style="color:#c0c4cc">
											{{ item.sopItemValue? '':'请选择' }}
										</view>
									</view>
									<!-- <u--input
										v-model="item.sopItemValue"
										border="none"
										placeholder="请选择"
										readonly
										class="minInput"
										:class="
											sizeColorHandle1(
												item.sopItemValueId,
												item.sopItem.details ?? []
											)
										"></u--input> -->
									<image
										@click.stop="clearData(index)"
										v-show="item.sopItemValueId"
										src="@/static/image/delete.png"
										class="deleteIcon" />
								</view>
								<text
									v-else
									:class="
										sizeColorHandle1(
											item.sopItemValueId,
											item.sopItem.details ?? []
										)
									">
									{{ item.sopItemValue || "" }}
								</text>
							</view>
						</view>

						<!-- 待需要上传图片后启用 -->
						<view
							class="mt-8 flex"
							v-if="item.sopItem.sopImage == '1'">
							<upload-image
								:multiple="false"
								:key="index"
								:keys="index"
								@fileChange="(list) => getImages(list, index)"
								:list="item.images ?? []"
								:capture="capture"
								:isRead="
									props.isRead == 1 ? true : false
								"></upload-image>
						</view>
					</view>
					<u-divider
						v-show="
							index + 1 !== taskInfo?.details.length
						"></u-divider>
				</view>
			</view>

			<!-- 提交 -->
			<view class="flex controls">
				<button class="submit_btn flex-1" @click="restData">
					重置
				</button>
				<button
					type="primary"
					@click="saveSubmit"
					class="submit_btn flex-1">
					保 存
				</button>
			</view>
		</view>
		<!-- </u--form> -->
		<!-- 空数据 -->
		<view v-show="formData.id == '' && activeTab === 0">
			<u-empty
				mode="list"
				icon="http://cdn.uviewui.com/uview/empty/list.png"></u-empty>
		</view>

		<view v-if="activeTab === 1" style="padding: 5px 10px">
			<!-- 已点检 -->
			<!-- 搜索&扫码 -->
			<view class="upload-scan" style="display: flex; padding: 5px 10px">
				<u-search
					v-model="searchContent"
					shape="round"
					bgColor="#fff"
					:show-action="true"
					actionText="搜索"
					:animation="true"
					class="search"
					@search="searchList"
					@clear="searchList"
					@custom="searchList"></u-search>
				<u-icon
					name="scan"
					size="27px"
					:bold="false"
					@click="openScan"></u-icon>
			</view>
			<view v-if="activeTab === 1" class="activeHeight">
				<!-- 列表 -->

				<u-list
					@scrolltolower="finishScrolltolower"
					class="list"
					v-if="finishTotal"
					:height="listHeight"
					lowerThreshold="50px">
					<u-list-item
						v-for="(item, index) in finishTableList"
						:key="item.id"
						@click="
							jumpEdit(
								'/qms/qmsCheckList/qmsFinishedInspection',
								item.id,
								'1',
								props.inspectionName
							)
						">
						<view class="card">
							<!-- 三种状态：待点检，点检中，点检完成 -->
							<view
								class="flex flex-col justify-between flex-1 h-full">
								<view class="record-content">
									<view class="record-item record-title">
										<text>{{ item.orderNo }}</text>
										<text>
											{{
												item?.orderStatus == 0
													? "未提交"
													: item?.orderStatus == 1
													? "未提交"
													: item?.orderStatus == 2
													? "已提交"
													: item?.orderStatus == 3
													? "异常"
													: ""
											}}
										</text>
									</view>
									<view class="record-item">
										<!-- <text>设备编号</text>
										<text>{{ item?.assetCode || "--" }}</text> -->
										<!-- <text>设备编号</text> -->
										<text>{{ item?.assetName || "" }}</text>
										<text>
											{{ item?.locationName || "" }}
										</text>
									</view>
									<view
										class="record-item"
										v-if="item?.sopTypeCode">
										<text>
											{{ item?.sopTypeName || "" }}
										</text>
										<text>
											{{ item?.sopTypeCode || "" }}
										</text>
									</view>
									<view
										class="record-item"
										v-if="item?.problemDesc">
										<text>问题描述</text>
										<text>
											{{ item?.problemDesc || "" }}
										</text>
									</view>
									<view class="record-item">
										<!-- 	<text>完成时间</text>
										<text>{{ item?.workTime || "" }}</text> -->
										<view style="display: flex">
											<!-- <text style="color: red">异常</text>
											<text style="color: red">
												({{
													item?.exceptionNumber ||
													"0"
												}})
												
											</text> -->
											<text>提交时间</text>
										</view>
										<text>{{ item?.workTime || "0" }}</text>
									</view>
									<!-- <view class="record-item">
										<text>点检标准</text>
										<text>
											{{ item?.sopStandardName || "--" }}
										</text>
									</view> -->
									<!-- <view class="record-item">
										<text>点检结果</text>
										<text
											:class="
												item?.checkResult == 0
													? 'red-color'
													: ''
											">
											{{
												item?.checkResult == 0
													? "不合格"
													: item?.checkResult == 1
													? "合格"
													: "无"
											}}
										</text>
									</view> -->
								</view>
							</view>
						</view>
					</u-list-item>
				</u-list>
				<view class="flex justify-center" v-show="finishTableLoading">
					<u-loading-icon color="#000a7b"></u-loading-icon>
				</view>
				<!-- 空数据 -->
				<view
					v-show="
						finishTableList.length === 0 && !finishTableLoading
					">
					<u-empty
						mode="list"
						icon="http://cdn.uviewui.com/uview/empty/list.png"></u-empty>
				</view>
			</view>
		</view>
		<!-- 空数据 -->
		<!-- <view v-show="formData.id == '' && activeTab === 1">
			<u-empty
				mode="list"
				icon="http://cdn.uviewui.com/uview/empty/list.png"></u-empty>
		</view> -->
		<!-- 检验项 -->
		<u-picker
			ref="uPickerRef"
			:show="showInspectionItems"
			:columns="actions"
			@cancel="showInspectionItems = false"
			@confirm="handelInspectionItems"
			keyName="checkItemName"></u-picker>
		<!-- 工序 -->
		<u-picker
			ref="uPickerRefProcess"
			:show="showProcessDataList"
			:columns="actionsPrpcess"
			@cancel="showProcessDataList = false"
			@confirm="handelProcessDataList"
			keyName="processName"></u-picker>
		<!-- 实际值选择picker -->
		<u-picker
			:show="showPicker"
			:columns="currentColumns"
			:defaultIndex="defaultIndex"
			keyName="text"
			immediateChange
			@cancel="showPicker = false"
			@confirm="changeData"></u-picker>
		<u-modal
			:show="showInfo"
			:content="contentInfo"
			@confirm="showInfo = false"></u-modal>
	</view>
</template>

<script setup lang="ts">
import { sizeColorHandle1, sizeColorHandle } from "@/utils/common";
import { nextTick, ref, toRefs, watch, reactive, computed, toRaw } from "vue";
import UploadImage from "@/components/upload-image-ssr.vue";
import { onShow, onPullDownRefresh, onLoad } from "@dcloudio/uni-app";
import { http } from "@/utils/http";
import { v4 as uuidv4 } from "uuid";
// import { PageResult, TabInfo } from "@/types/type";
import { getSecondTime } from "@/utils/common";
// import ScanBar from "@/components/scan-bar/index.vue";
import ScanBar from "@/pages/qms/qmsCheckList/components/qmsScanBar.vue";
// 获取传回的参数
const listHeight = ref("");
onLoad(() => {
	// #ifdef H5
	listHeight.value = "calc(100vh - 84px - 44px)";
	// #endif

	// #ifdef APP-PLUS
	listHeight.value = "calc(100vh - 44px - 44px)";
	// #endif
	uni.setNavigationBarTitle({
		title: props.inspectionName, // 设置为你想要显示的文字
	});
});
const props = defineProps({
	inspectionId: {
		type: String,
		default: "",
	},
	inspectionName: {
		type: String,
		default: "", //
	},
	isRead: {
		type: String,
		default: "0",
	},
});
// -------------------------选择框列表-----------------
// 是否展示下拉列表

const showInspectionItems = ref(false);
// 表单
const formData = reactive({
	id: "",
	checkItemName: "",
	checkItemCode: "",
	standardId: "",
	standardCode: "",
	standardName: "",
});
// 选择下拉列表的数据
const handelInspectionItems = (e) => {
	restTaskInfo();
	// 选择的数据进行赋值
	let item = e.value[0];
	formData.id = item.id;
	formData.checkItemName = item.checkItemName;
	formData.checkItemCode = item.checkItemCode;
	formData.standardId = item.standardId;
	formData.standardCode = item.standardCode;
	formData.standardName = item.standardName;
	if (activeTab.value === 0 && formData.id) {
		// 检验
		console.log("检验");
		getCheckItemList();
	} else if (activeTab.value === 1 && formData.id) {
		// 已检
		console.log("已检");
		finishTableList.value = [];
		finishLoadmore();
		showInspectionItems.value = false;
	}
};
// 点击输入框操作
const getInspectionItems = () => {
	getInspectionList();
};
// 下拉列表
const actions = reactive([]);
// 获取下拉列表接口
async function getInspectionList() {
	await http({
		method: "POST",
		url: `/qms/qmscheckitem/getList`,
		data: {
			sopTypeCode: props.inspectionId,
		},
	}).then((res) => {
		actions[0] = res.data;
		showInspectionItems.value = true;
		// uni.hideKeyboard();
	});
}
// 获取下拉列表接口
async function getCheckItemList() {
	await http({
		method: "POST",
		url: `/qms/qmspointcheckorder/newPointCheckOrder`,
		data: {
			id: formData.id,
			standardId: formData.standardId,
			standardCode: formData.standardCode,
			standardName: formData.standardName,
		},
	}).then((res) => {
		taskInfo = Object.assign(taskInfo, res.data);
		resetMaterialFields();
		codeMaterial.value = "";
		resetTowFields();
		codeBatch.value = "";
		resetSerialFields();
		codeSerial.value = "";
		console.log(taskInfo);
		showInspectionItems.value = false;
	});
}

// 获取已检下拉列表接口
// 已点检数据
const finishTableList = ref([]);
const finishTableLoading = ref(false);
const finishCurrent = ref(0);
const finishTotalPage = ref(0);
const finishTotal = ref(0);

// 已完成列表获取
async function finishLoadmore(type?: string) {
	console.log(props.inspectionId);
	try {
		finishCurrent.value += 1;
		finishTableLoading.value = true;
		const result: PageResult<any> = await http({
			method: "POST",
			url: "/qms/qmspointcheckorder/queryMyList",
			data: {
				current: finishCurrent.value,
				size: 20,
				data: {
					isChecked: "1",
					sopTypeCode: props.inspectionId ? props.inspectionId : "",
					orderNo: searchContent.value,
				},
			},
		}).finally(() => {
			finishTableLoading.value = false;
		});
		const list = result.data;
		for (let i = 0; i < list.length; i++) {
			finishTableList.value.push(list[i]);
		}
		finishTotalPage.value = result.page;
		finishTotal.value = result.total;
		type === "refresh" && uni.stopPullDownRefresh();
	} catch (e) {
		console.log(e);
	}
}
const finishScrolltolower = () => {
	finishTotalPage.value > finishCurrent.value && finishLoadmore();
};
onPullDownRefresh(() => {
	searchContent.value = "";
	searchList();
});
// 列表搜索
function searchList() {
	// 已点检
	if (formData.id) {
		finishCurrent.value = 0;
		finishTableList.value = [];

		finishLoadmore("refresh");
	} else {
		uni.stopPullDownRefresh();
	}
}
// -------------------------检验及已检状态列表-----------------
// tabs列表
const activeTab = ref(0);
const tabList = [
	{ name: "检验", value: 0 },
	{ name: "已检", value: 1 },
];
function changeTab(val) {
	console.log(val);
	activeTab.value = val.index;

	if (val.index === 0 && formData.id) {
		// 检验
		console.log("检验");
		getCheckItemList();
	} else if (val.index === 1 && formData.id) {
		// 已检
		console.log("已检");
		finishTableList.value = [];
		finishLoadmore();
	}
}
// -------------------------扫码输入框-----------------
//扫码输入框--物料编号
const scanBarMaterialRef = ref();
const dateMaterialkey = ref(uuidv4());
// const dateMaterialkey = ref("1");
function resetMaterialFields() {
	dateMaterialkey.value = uuidv4();
	// dateMaterialkey.value = "1";
	// scanBarMaterialRef.value.reset();
}
// 获取扫码结果--物料编码
const codeMaterial = ref("");
function getScanResultMaterial(result) {
	codeMaterial.value = result;

	if (result) {
		getMaterialDataList(codeMaterial.value);
	}
}
// watch(
// 	() => scanBarMaterialRef.value,
// 	(newVal) => {
// 		console.log("codeMaterial.value", newVal);
// 	}
// );

// 根据设备编号查询设备信息
const getMaterialDataList = async (code) => {
	if (code) {
		await http({
			method: "GET",
			url: `/mdm/mdmmaterial/get/code/${code} `,
			data: {},
		}).then((res) => {
			if (res.data) {
				taskInfo.materialId = res.data.id;
				taskInfo.materialName = res.data.materialName;
				taskInfo.materialCode = res.data.materialCode;
			}else{
				uni.showToast({
					title: "物料编码不存在！",
					icon: "none",
				});
				resetMaterialFields()
				codeMaterial.value=''
				taskInfo.materialId = '';
				taskInfo.materialName = '';
				taskInfo.materialCode = '';
			}
		});
	}
};
//扫码输入框--批次号
const scanBarBatchRef = ref();
const dateBatchkey = ref(uuidv4());
// const dateBatchkey = ref("2");
function resetTowFields() {
	// dateBatchkey.value = uuidv4();
	dateBatchkey.value = "2";
}
// 获取扫码结果--批次号
const codeBatch = ref("");
function getScanResultBatch(result) {
	console.log("22");
	codeBatch.value = result;
}
//扫码输入框--序列号
const scanBarSerialRef = ref();
const dateSerialkey = ref(uuidv4());
// const dateSerialkey = "3";
function resetSerialFields() {
	dateSerialkey.value = "3";
	dateSerialkey.value = uuidv4();
}
// 获取扫码结果--序列号
const codeSerial = ref("");
function getScanResultSerial(result) {
	codeSerial.value = result;
}
//扫码输入框--设备
const scanBarEquipRef = ref();
const dateEquipkey = ref(uuidv4());
// const dateEquipkey = ref("4");
function resetEquipFields() {
	dateEquipkey.value = uuidv4();
	// dateEquipkey.value = "4";
}
// 获取扫码结果--设备
const codeEquip = ref("");
function getScanResultEquip(result) {
	if(result){
		codeEquip.value = result;
		getEquipDataList(codeEquip.value);
	}else{
		resetEquipFields()
		codeEquip.value=''
		taskInfo.equipId ='';
		taskInfo.equipName = '';
		taskInfo.equipCode = '';
		}
}
// 根据设备编号查询设备信息
const getEquipDataList = async (code) => {
	await http({
		method: "GET",
		url: `/mdm/mdmequipinfo/getByCode/${code} `,
		data: {},
	}).then((res) => {
		if (res.data) {
			taskInfo.equipId = res.data.id;
			taskInfo.equipName = res.data.equipName;
			taskInfo.equipCode = res.data.equipCode;
		}
			
		
	}).catch(()=>{
		uni.showToast({
			title: "设备不存在！",
			icon: "none",
		});
		resetEquipFields()
		codeEquip.value=''
		taskInfo.equipId ='';
		taskInfo.equipName = '';
		taskInfo.equipCode = '';
	})
};

// 工序列表
// 是否展示下拉列表
const showProcessDataList = ref(false);
// 选择下拉列表的数据
const handelProcessDataList = (e) => {
	// 选择的数据进行赋值
	let item = e.value[0];
	taskInfo.processId = item.id;
	taskInfo.processName = item.processName;
	taskInfo.processCode = item.processCode;
	showProcessDataList.value = false;
};
// 点击输入框操作
const getProcessData = () => {
	getProcessDataList();
};
// 下拉列表
const actionsPrpcess = reactive([]);
const getProcessDataList = async () => {
	await http({
		method: "POST",
		url: `/mdm/mdmprocess/getList`,
		data: {},
	}).then((res) => {
		actionsPrpcess[0] = res.data;
		showProcessDataList.value = true;
		uni.hideKeyboard();
	});
};

// -------------------------检验项-----------------
let taskInfo = reactive({
	assetCode: "",
	assetId: "",
	assetKind: "",
	assetName: "",
	checkItemId: "",
	checkItemName: "",
	checkPersonId: "",
	checkPersonName: "",
	checkResult: "",
	checkRuleId: "",
	checkTypeName: "",
	classes: "",
	createTime: "",
	createdUserId: "",
	createdUserName: "",
	deleted: "",
	details: [],
	effectCheckCode: "",
	effectPreCheckCode: "",
	effectTypeCode: "",
	enabled: "",
	epCheckPreId: "",
	equipCode: "",
	equipId: "",
	equipName: "",
	executePersonId: "",
	executePersonName: "",
	id: "",
	ids: "",
	lastUpdateTime: "",
	lastUpdatedUserId: "",
	lastUpdatedUserName: "",
	materialBatch: "",
	materialCode: "",
	materialId: "",
	materialName: "",
	materialSerial: "",
	needEquip: 0,
	needMaterialBatch: 1,
	needMaterialSerial: 1,
	needProcess: 1,
	opeTypeCode: "",
	orderNo: "",
	orderStatus: 0,
	planId: "",
	planName: "",
	planNo: "",
	planTime: "",
	processCode: "",
	processId: "",
	processName: "",
	productTime: "",
	projectName: "",
	remarks: "",
	sopStandardCode: "",
	sopStandardId: "",
	sopStandardName: "",
	sopTypeCode: "",
	sopTypeId: "",
	sopTypeName: "",
	standardId: "",
	version: "",
	workTime: "",
});

function restTaskInfo() {
	taskInfo.assetCode = "";
	taskInfo.assetId = "";
	taskInfo.assetKind = "";
	taskInfo.assetName = "";
	taskInfo.checkItemId = "";
	taskInfo.checkItemName = "";
	taskInfo.checkPersonId = "";
	taskInfo.checkPersonName = "";
	taskInfo.checkResult = "";
	taskInfo.checkRuleId = "";
	taskInfo.checkTypeName = "";
	taskInfo.classes = "";
	taskInfo.createTime = "";
	taskInfo.createdUserId = "";
	taskInfo.createdUserName = "";
	taskInfo.deleted = "";
	taskInfo.details = [];
	taskInfo.effectCheckCode = "";
	taskInfo.effectPreCheckCode = "";
	taskInfo.effectTypeCode = "";
	taskInfo.enabled = "";
	taskInfo.epCheckPreId = "";
	taskInfo.equipCode = "";
	taskInfo.equipId = "";
	taskInfo.equipName = "";
	taskInfo.executePersonId = "";
	taskInfo.executePersonName = "";
	taskInfo.id = "";
	taskInfo.ids = "";
	taskInfo.lastUpdateTime = "";
	taskInfo.lastUpdatedUserId = "";
	taskInfo.lastUpdatedUserName = "";
	taskInfo.materialBatch = "";
	taskInfo.materialCode = "";
	taskInfo.materialId = "";
	taskInfo.materialName = "";
	taskInfo.materialSerial = "";
	taskInfo.needEquip = "";
	taskInfo.needMaterialBatch = "";
	taskInfo.needMaterialSerial = "";
	taskInfo.needProcess = "";
	taskInfo.opeTypeCode = "";
	taskInfo.orderNo = "";
	taskInfo.orderStatus = "";
	taskInfo.planId = "";
	taskInfo.planName = "";
	taskInfo.planNo = "";
	taskInfo.planTime = "";
	taskInfo.processCode = "";
	taskInfo.processId = "";
	taskInfo.processName = "";
	taskInfo.productTime = "";
	taskInfo.projectName = "";
	taskInfo.remarks = "";
	taskInfo.sopStandardCode = "";
	taskInfo.sopStandardId = "";
	taskInfo.sopStandardName = "";
	taskInfo.sopTypeCode = "";
	taskInfo.sopTypeId = "";
	taskInfo.sopTypeName = "";
	taskInfo.standardId = "";
	taskInfo.version = "";
	taskInfo.workTime = "";
	resetMaterialFields();
	codeMaterial.value = "";
	resetTowFields();
	codeBatch.value = "";
	resetSerialFields();
	codeSerial.value = "";
	resetEquipFields();
	codeEquip.value = "";
}

// 作业方法提示框
const showInfo = ref(false);
const contentInfo = ref("");
function openInfoModal(data) {
	showInfo.value = true;
	contentInfo.value = `作业方法：${data}`;
}
// 摄像头参数设置
const capture = ref(["camera", "album"]);
// 获取图片
function getImages(list, index) {
	const current = JSON.parse(JSON.stringify(taskInfo.details))[index];
	current.images = [];
	list?.forEach((item) => {
		current.images.push(item);
	});
	const elder = JSON.parse(JSON.stringify(taskInfo.details));
	elder[index] = current;
	taskInfo.details = elder;
}

// 获取现场图片
function getSceneImages(list) {
	taskInfo.images = list;
}

/**
 * 选择实际值
 */
const showPicker = ref(false);
const currentColumns = reactive([[]]);
const currentIndex = ref();
const defaultIndex = ref([0]);

/**
 * 组织键盘弹出
 */
function hideKeyboard(index, list) {
	const lists = JSON.parse(JSON.stringify(list));
	lists.forEach((item) => {
		console.log("item", item);
		item.text = `${item.standardValue} - ${
			item.resultValue == "1" ? "正常" : "异常"
		}`;
	});
	currentIndex.value = index;
	currentColumns[0] = lists ?? [];
	// 给原有的值,赋值picker默认index
	const sopItemValueId = taskInfo.details[index].sopItemValueId;
	if (sopItemValueId) {
		const currentIndex = list.findIndex(
			(self) => self.id === sopItemValueId
		);
		defaultIndex.value = [currentIndex];
	} else {
		defaultIndex.value = [0];
	}
	showPicker.value = true;
	uni.hideKeyboard();
}
function clearData(index) {
	taskInfo.details[index].sopItemValueId = "";
	taskInfo.details[index].sopItemValue = "";
	showPicker.value = false;
}
function changeData(e) {
	console.log("e", e.value[0]);
	taskInfo.details[currentIndex.value].sopItemValue =
		e.value[0].standardValue;
	taskInfo.details[currentIndex.value].sopItemValueId = e.value[0].id;
	taskInfo.details[currentIndex.value].sopItemResult = e.value[0].resultValue;
	showPicker.value = false;
}

// 重置
const restData = () => {
	getCheckItemList();
};

// 保存
const saveLoading = ref(false);
async function saveSubmit() {
	// 校验是否录入工装编号成功
	taskInfo.materialSerial = codeSerial.value;
	taskInfo.materialBatch = codeBatch.value;
	taskInfo.materialCode = codeMaterial.value;
	taskInfo.equipCode = codeEquip.value;
	if (!taskInfo.materialCode) {
		uni.showToast({
			title: "物料编码不能为空",
			icon: "none",
		});
		return false;
	}

	if (!taskInfo.materialBatch && taskInfo.needMaterialBatch) {
		uni.showToast({
			title: "批次号不能为空",
			icon: "none",
		});
		return false;
	}
	if (!taskInfo.materialSerial && taskInfo.needMaterialSerial) {
		uni.showToast({
			title: "序列号不能为空",
			icon: "none",
		});
		return false;
	}
	if (!taskInfo.equipCode && taskInfo.needEquip) {
		uni.showToast({
			title: "设备不能为空",
			icon: "none",
		});
		return false;
	}
	if (!taskInfo.processId && taskInfo.needProcess) {
		uni.showToast({
			title: "工序不能为空",
			icon: "none",
		});
		return false;
	}
	// 校验判断数字
	for (let i = 0; i < taskInfo.details.length; i++) {
		if (taskInfo.details[i].sopItem.standardValue == "1") {
			if (isNaN(Number(taskInfo.details[i].sopItemValue))) {
				uni.showToast({
					title: `${taskInfo.details[i].sopItemName}的实际值只能为数字`,
					icon: "none",
				});
				return false;
			}
		}
		console.log(taskInfo.details[i].sopItemValue);
		if (taskInfo.details[i].sopItemValue == null||taskInfo.details[i].sopItemValue == '') {
			uni.showToast({
				title: `${taskInfo.details[i].sopItemName}的实际值不能为空`,
				icon: "none",
			});
			return false;
		}
	}

	// 校验是否录入工装编号成功
	console.log(toRaw(taskInfo));
	// // save提交
	saveLoading.value = true;
	const result: any = await http({
		method: "POST",
		url: `/qms/qmspointcheckorder/submit `,
		data: toRaw(taskInfo),
	}).catch(() => {
		saveLoading.value = false;
	});
	if (result?.code == 200) {
		uni.showToast({
			title: "保存成功",
		});
		// restTaskInfo();
		formData.id = "";
		formData.checkItemName = "";
		formData.checkItemCode = "";
		formData.standardId = "";
		formData.standardCode = "";
		formData.standardName = "";
	}
	saveLoading.value = false;
}

// -------------------------已检验项-----------------

// 点击扫码
const searchContent = ref("");
function openScan() {
	/* #ifdef H5 */
	uni.showToast({
		icon: "none",
		title: "请到安卓设备进行扫码",
	});
	/* #endif */
	// 只允许通过相机扫码
	uni.scanCode({
		scanType: ["barCode", "qrCode"],
		onlyFromCamera: true,
		success: function (res) {
			searchContent.value = JSON.stringify(res.result);
			searchList();
			console.log("条码类型：" + res.scanType);
			console.log("条码内容：" + res.result);
		},
	});
}

// 点击查看
function jumpEdit(item = "", id = "", Read = "0", inspectionName = "") {
	uni.navigateTo({
		url: `/pages${item}?id=${id}&isRead=${Read}&inspectionName=${inspectionName}`,
		fail: () => {
			uni.showToast({
				title: "敬请期待",
				icon: "error",
			});
		},
	});
}
</script>

<style lang="scss" scoped>
@import "@/static/css/standard_item.scss";
uni-page-body {
	height: 100%;
	background-color: #fbfbff;
}
.search {
	:deep(.u-search__content) {
		box-shadow: 0 1px 8px 0px rgb(19 10 236 / 8%);
	}
}

.tab-bar {
	padding: 0 20px;
}
.upload-scan {
	padding: $uni-page-padding;
	padding-top: 5px;
	padding-bottom: 5px;
	display: flex;
	align-items: center;
	// box-shadow: 0 1px 10px 0px rgb(19 10 236 / 10%);
	.u-icon {
		margin-left: 8px;
	}
}
.u-line {
	padding: 10px 0;
}

:deep(.u-form-item__body) {
	padding: 4px 0 12px;
}
:deep(.u-form-item) {
	.u-form-item__body__left__remarks__label {
		color: #a8b2bb;
		font-size: 14px;
		margin-bottom: 1px;
	}
	.u-form-item__body__left__remarks__required {
		font-size: 16px;
	}

	.u-textarea--disabled {
		background-color: #fff !important;
	}
	.uni-textarea-textarea {
		color: #16254e !important;
	}

	.input-box {
		width: 100%;
		border: 0.5px solid #dadbde !important;
		border-radius: 4px;
		.u-input {
			pointer-events: none;
			height: 34px;
			padding-left: 10px !important;
		}
		image {
			padding: 7px 6px;
			width: 20px;
			height: 20px;
		}
	}
}
.overhidden {
	overflow: hidden;
}

.controls {
	padding: 0 5px;
	position: fixed;
	bottom: 5px;
	left: 0;
	width: calc(100% - 10px);
	z-index: 99;
	.submit_btn {
		margin: 0px 5px;
		border-radius: 10px;
	}
}
.grey_btn {
	width: 50%;
	margin: 60rpx 20rpx;
	border-radius: 60rpx;
	background-color: #fff !important;
	border: 1px solid #999;
	box-shadow: 0 1px 10px rgba(22, 37, 78, 0.08);
	color: #4f4f4f;
}

:deep(.record-remarks1) {
	border: none;
	background-color: #fff !important;
	border-radius: 4px;
	box-shadow: 0 1px 8px 0px rgb(19 10 236 / 8%);
	padding: 0 10px;
	.u-form-item__body {
		padding: 5px 0 5px;
	}
	.uni-input-placeholder {
		color: gray !important;
	}
}
.searchScan_div {
	padding: 0px 0px !important;
	width: 80%;
}
:deep(.u-tabs__wrapper__nav__item) {
	height: 38px !important;
}

.list {
	.card {
		margin: $uni-page-padding;
		margin-top: 4px;
		margin-bottom: 4px;
		border-radius: 7px;
		padding: 7px 10px 7px 10px;
		transition: all 300ms ease;
		box-shadow: 0 1px 5px rgba(19, 11, 236, 0.13);
		background-color: #fff;
		.card-flex {
			display: flex;
			align-items: flex-start;
		}
		.right {
			flex: 1;
		}
		.title {
			font-size: 14px;
			line-height: 40px;
			height: 40px;
			margin-top: 8px;
			font-weight: bold;
			color: #101010;
			border-top: 1px solid #e5ebff;
		}
		.uni-btn {
			float: right;
			font-size: 12px;
			height: 24px;
			line-height: 24px;
			padding: 0 15px;
			margin-top: 7px;
			font-weight: 400;
		}
		.des {
			font-size: 12px;
			line-height: 20px;
			// height: 20px;
			margin-bottom: 3px;
		}
		.left {
			// flex: 1;
			flex-basis: calc(50% + 10px);
		}
	}
}

.record-title {
	font-size: 15px;
	font-weight: bold;
	// margin: 10px 0 !important;
}
.record-footer {
	// height: 30px;
	display: flex;
	justify-content: flex-end;
	align-items: center;
	margin: 10px 0 17px;
	.record-btn {
		height: 30px;
		outline: none;
		border: none;
		line-height: 30px;
		border-radius: 6px;
		font-size: 14px;
		margin: 0px;
		margin-left: 10px;
		padding: 0 12px;
	}
	.reach {
		background-color: $primary-btn-color;
		color: #ffffff;
	}
	.canel {
		background-color: #ffffff;
		color: $primary-btn-color;
		// border: 1px solid $primary-btn-color;
	}
	.complete {
		background-color: #ffffff;
		color: $primary-btn-color;
		border: 1px solid $primary-btn-color;
	}
}
.u-loading-icon {
	margin: 8px 0;
}
.record-content .record-item > :first-child {
	min-width: 79px;
}

.addBox {
	padding: 5px 10px;
	height: 100%;
	.addBoxMaterial {
		display: flex;
		align-items: center;
		margin-top: 10px;
		.addBoxMaterialTitle {
			font-size: 15px;
			margin-right: 5px;
		}
	}
	.addBoxSerial {
		display: flex;
		align-items: center;
		.addBoxSerialTitle {
			font-size: 15px;
			margin-left: 15px;
			margin-right: 5px;
		}
	}
	.addBoxBatch {
		display: flex;
		align-items: center;
		.addBoxBatchTitle {
			font-size: 15px;
			margin-left: 15px;
			margin-right: 5px;
		}
	}
	.addBoxProcess {
		display: flex;
		align-items: center;
		justify-content: start;
		margin-top: -10px;
		.addBoxProcessTitle {
			font-size: 15px;
			width: 40px;
			text-align: end;
			margin-left: 20px;
		}
	}
	.addBoxEquip {
		display: flex;
		align-items: center;
		margin-top: -6px;
		.addBoxEquipTitle {
			font-size: 15px;
			margin-left: 30px;
			margin-right: 5px;
		}
	}
}
.addBoxTitle {
	display: flex;
	justify-content: center;
	align-items: center;
	margin-top: 10px;
	font-weight: 700;
}
</style>
